package com.thirteengroup.casemg.controller;



import com.thirteengroup.casemg.domain.*;
import com.thirteengroup.casemg.service.ICustomerService;
import com.thirteengroup.casemg.service.IDdetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/customer")
public class customerController {

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private IDdetailService ddetailService;

    @RequestMapping("/list")
    @ResponseBody
    public JasonResult listAll(queryObject queryObject){
        if("全部客户".equals(queryObject.getQuerystatus())){
            queryObject.setQuerystatus(null);
        }
        int count= customerService.selectCount(queryObject);
        List<customer> customers = customerService.queryCustomer(queryObject);
        PageResult<customer> pageResult=new PageResult<>(queryObject.getCurrentPage(),count, queryObject.getPageSize(),customers);
        JasonResult jasonResult=new JasonResult("查询客户成功",true,pageResult);
        return jasonResult;
    }


    @ResponseBody
    @RequestMapping("/upOrin")
    public JasonResult upOrin(customer customer){


        if(customer.getId()==null){
            //insert
            /*
            查询dl的目的是获得dl中的sequence，dl的sequence是插入customer的需要的
             */
            Integer dlid=customer.getDlid();
            ddetail dl=ddetailService.queryOne(dlid);
            customer.setDl(dl);
            customerService.insertOne(customer);
            JasonResult jasonResult=new JasonResult("插入成功",true);
            return jasonResult;
        }else {//update
            Integer dlid=customer.getDlid();
            ddetail dl=ddetailService.queryOne(dlid);
            customer.setDl(dl);
            customerService.updateOne(customer);
            JasonResult jasonResult=new JasonResult("更新成功",true);
            return jasonResult;
        }
    }

    @ResponseBody
    @PostMapping("/delete")
    public JasonResult deleteByid(Long id){
        customerService.deleteByid(id);
        JasonResult jasonResult=new JasonResult("删除成功",true);
        return jasonResult;
    }

    @GetMapping("/statistics")
    @ResponseBody
    public JasonResult statisticsAll(queryObject queryObject){
        String type = queryObject.getType();
        System.out.println("+++++++++"+type);
        if("inputUser".equals(type)||type==null||type==""){
            List<statisticsQuery> statisticsQueryList = customerService.statisticsInputUser(queryObject);
            Integer totalCount=statisticsQueryList.size();
            PageResult<statisticsQuery> pageResult=new PageResult<>(queryObject.getCurrentPage(),totalCount
                    , queryObject.getPageSize(), statisticsQueryList);
            JasonResult jasonResult=new JasonResult("员工统计",true,pageResult);
            return jasonResult;
        }else if("time".equals(type)){
            List<statisticsQuery> statisticsQueryList = customerService.statisticsTime(queryObject);
            Integer totalCount=statisticsQueryList.size();
            PageResult<statisticsQuery> pageResult=new PageResult<>(queryObject.getCurrentPage(),totalCount
                    , queryObject.getPageSize(), statisticsQueryList);
            JasonResult jasonResult=new JasonResult("时间统计",true,pageResult);
            return jasonResult;
        }
        return null;

    }



    @ResponseBody
    @RequestMapping("/getDdetail")
    public JasonResult getDdetail(String dmname){
        List<ddetail> ddetailList=ddetailService.queryAll(dmname);
        JasonResult jasonResult=new JasonResult("职业获取成功",true,ddetailList);
        return jasonResult;
    }


}
